/*
 * Created by Weiting on 2011/11/11
 * copyright 2011 III. All rights reserved.
 *
 */
package com.eslserver.action.coordinator;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.eslserver.core.aware.CoordinatorAware;
import com.eslserver.dao.DAOException;
import com.eslserver.dao.ESLServerDAOFactory;
import com.eslserver.dao.CoordinatorDAO;
import com.eslserver.to.Coordinator;
import com.eslserver.util.JSONResponseUtil;

@SuppressWarnings("serial")
public class LoadCoordinatorInfo extends HttpServlet implements
		CoordinatorAware {

	public static final Logger LOG = LoggerFactory
			.getLogger(LoadCoordinatorInfo.class);

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		PrintWriter writer = resp.getWriter();
		try {

			Coordinator coor = Coordinator.createESLCoordinator();
			String coorID = req.getParameter(PARAM_COORDINATOR_ID);

			if (coorID != null && coorID.length() != 0) {
				CoordinatorDAO coorDAO = ESLServerDAOFactory.getDAOFactory(
						ESLServerDAOFactory.ESLSERVER_DAOFACTORY)
						.getCoordinatorDAO();
				coor = coorDAO.loadCoordinatorByID(Integer.parseInt(coorID));
			}
			writer.println(JSONResponseUtil.getJSONObjectString(coor));
		} catch (DAOException e) {
			LOG.error("error laoding ESL Info", e);
		} finally {
			writer.close();
		}
	}

}
